package com.yugao.fintech.antelope.infra.idgenerator.controller;

import com.yugao.fintech.antelope.infra.idgenerator.AutoRegisterWorkerId;
import com.yugao.fintech.framework.core.response.Resp;

/**
 * id生成器相关接口
 */
@RestController
@RequestMapping("/idGen")
public class IdGenController {
    @Autowired
    private AutoRegisterWorkerId autoRegisterWorkerId;

    /**
     * 获取id生成器的下一个workId
     * @return -1或者为空则生成失败
     */
    @GetMapping(value = "/getNextWork")
    public Resp<Short> getIdGenNextWorkId() {
        return Resp.ok(autoRegisterWorkerId.getNextWorkId());
    }

    /**
     * 刷新id生成器workId被占用时间
     * @param workId 当前服务所被分配的workId值
     */
    @PostMapping(value = "/refreshWorkId")
    public Resp<Boolean> refreshIdGenWorkId(@RequestParam("workId") Short workId) {
        return Resp.ok(autoRegisterWorkerId.refresh(String.valueOf(workId)));
    }
}
